<?php

class Article_model extends CI_Model{
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    private $tbl = "article";
    function num_rows() {
        $result = $this->db->get($this->tbl);
        return $result->num_rows();
    }
    function get_all($limit, $offset){
        
        $this->db->select('ar.id_article, ar.title, ar.source, ar.date, ar.view , u.url_image, u.id_sex, u.first_name, u.last_name, u.email');
        $this->db->from($this->tbl." as ar");
        $this->db->join("article_of_center as aoc", "ar.id_article = aoc.id_article",'left');
        $this->db->join("center as ct", "aoc.id_center = ct.id_center",'left');
        $this->db->join('users as u', 'u.id_user = ar.id_author');
        
        $this->db->order_by('ar.date', 'DESC');
        
        $this->db->limit($limit, $offset);
        
        return  $this->db->get();
    }
    function get_by_type($id_type_center, $limit, $offset) {
        $this->db->select('ar.id_article, ar.title, ar.source, ar.date, ar.view, u.url_image, u.id_sex, u.first_name, u.last_name, u.email');
        $this->db->from($this->tbl." as ar");
        $this->db->join("article_of_center as aoc", "ar.id_article = aoc.id_article",'left');
        $this->db->join("center as ct", "aoc.id_center = ct.id_center",'left');
        $this->db->join('users as u', 'u.id_user = ar.id_author');
        $this->db->where("aoc.id_type_center",$id_type_center);
        
        $this->db->order_by('ar.date', 'asc');
        
        $this->db->limit($limit, $offset);
        
        return  $this->db->get();
    }
    function get_by_id($id) {
        $this->db->select('ar.id_article, ar.title, ar.source, ar.date, ar.view, u.url_image, u.id_sex, u.first_name, u.last_name, u.email');
        $this->db->from($this->tbl." as ar");
        $this->db->join("article_of_center as aoc", "ar.id_article = aoc.id_article",'left');
        $this->db->join("center as ct", "aoc.id_center = ct.id_center",'left');
        $this->db->join('type_center as tc', 'tc.id_type_center = ct.id_type_center','left');
        $this->db->join('users as u', 'u.id_user = ar.id_author');
        $this->db->where('ar.id_article',$id);
        
        $this->db->order_by('ar.date', 'asc');
        
        $this->db->limit(1);
        
        return  $this->db->get();
    }
    function increate_view($id,$current_view) {
        $this->db->update($this->tbl, array('view'=>$current_view+1), array('id_article'=>$id));
    }
    
    function getArticlesForHome()
    {
        $this->db->select('article.title, users.last_name, users.first_name, article.id_article, article.source, article.view');
        $this->db->from('article');
        $this->db->join('users', 'users.id_user = article.id_author');
        $this->db->order_by('date','DESC');
        $this->db->limit(4);
        $query = $this->db->get();
        if($query->num_rows()){
            return $query->result_array();
        }
        else{
            return NULL;
        }
    }
    // Lấy danh sách ảnh theo từng bài báo
    function getListImages($id_article)
    {
        $this->db->select('id_article, url_image_article, dicription');
        $this->db->from('image_article');
        $this->db->where('id_article', $id_article);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
        else{
            return NULL;
        }
    }
    // lấy các article có số views nhiều nhất
    function getTopViewArticle()
    {
        $this->db->select('article.title, article.id_article, article.source, article.view');
        $this->db->from($this->tbl);
        $this->db->order_by('view','DESC');
        $this->db->limit(4);
        $query = $this->db->get();
        if($query->num_rows()){
            return $query->result_array();
        }
        else{
            return NULL;
        }
    }

    //lấy các bài viết khác
    function getArticleDiff($id)
    {
        $this->db->select('ar.id_article, ar.title, ar.source, ar.date, ar.view, u.url_image, u.id_sex, u.first_name, u.last_name, u.email');
        $this->db->from($this->tbl." as ar");
        $this->db->join("article_of_center as aoc", "ar.id_article = aoc.id_article",'left');
        $this->db->join("center as ct", "aoc.id_center = ct.id_center",'left');
        $this->db->join('type_center as tc', 'tc.id_type_center = ct.id_type_center','left');
        $this->db->join('users as u', 'u.id_user = ar.id_author');
        $this->db->where('ar.id_article != ',$id);
        
        $this->db->order_by('ar.date', 'desc');
        
        return  $this->db->get();
    }
    
}

